package com.example.springioc;

import com.example.springioc.config.UserConfiguration;
import com.example.springioc.controller.UserController;
import com.example.springioc.dao.UserInfo;
import com.example.springioc.service.UserService;
import org.apache.catalina.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication

// @ComponentScan注解会更改扫描路径，SpringBootApplication的默认扫描路径为该类所在的包路径
@ComponentScan({"com.example"}) //更改扫描路径为com.example，spring会扫描路径中加了五大类注解的类
public class SpringIocApplication {

    public static void main1(String[] args) {
        ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);

        /*
                通过三种getBean方法从spring中拿到UserController对象
                1. getBean(UserController.class)
                2. getBean("userController")
                3. getBean("userController", UserController.class)
                其中userController为UserController存在spring中的name，一般name默认将类名改为小驼峰写法

                并且通过打印日志可以观察到，三种方法返回的是同一个对象（地址相同）
         */
        UserController userController = context.getBean(UserController.class);
        System.out.println(userController);
        userController.sayHello();
        System.out.println("-----------------------------");

        UserController userController1 = (UserController) context.getBean("userController");
        System.out.println(userController);
        userController.sayHello();
        System.out.println("-----------------------------");

        UserController userController2 = context.getBean("userController", UserController.class);
        System.out.println(userController);
        userController.sayHello();
        System.out.println("-----------------------------");

    }

    public static void main2(String[] args) {
        ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);


        UserService userService = context.getBean(UserService.class);
        System.out.println(userService);
        userService.sayHello();
        System.out.println("-----------------------------");

        UserService userService1 = (UserService) context.getBean("userService");
        System.out.println(userService1);
        userService1.sayHello();
        System.out.println("-----------------------------");

        UserService userService2 = context.getBean("userService",UserService.class);
        System.out.println(userService2);
        userService2.sayHello();
        System.out.println("-----------------------------");
    }

    public static void main3(String[] args) {

        ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
        // @Bean为方法注解，可以将方法中返回的对象存入spring中，此时对象名称为对应的方法名
        // 不同于五大类注解的是，@Bean 可以针对同⼀个类, 存入多个对象
//        UserInfo userInfo1 = context.getBean("userInfo1",UserInfo.class);
//        UserInfo userInfo2 = context.getBean("userInfo2",UserInfo.class);
//        System.out.println(userInfo1);
//        System.out.println(userInfo2);


        UserInfo userInfo1 = context.getBean("u1",UserInfo.class);
        UserInfo userInfo2 = context.getBean("u2",UserInfo.class);
        System.out.println(userInfo1);
        System.out.println(userInfo2);
    }


    public static void main4(String[] args) {
        ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
        UserController userController = context.getBean("userController",UserController.class);
        // @Autowired注解实现依赖注入的三种方式
        userController.say();
        System.out.println("-------------------");
        userController.say1();
        System.out.println("-------------------");
        userController.say2();

    }

    public static void main5(String[] args) {
        ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
        UserService userService = context.getBean("userService", UserService.class);
        userService.say();
        System.out.println("-------------");
        userService.say1();
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringIocApplication.class, args);
    }

}
